function [hfile, densm] = srf_Smooth(hfile, niter, force, opts)
% SRF::Smooth  - apply smoothing morph
%
% FORMAT:       [srf, densm] = srf.Smooth([niter [, force, opts]])
%
% Input fields:
%
%       niter       number of iterations, default 150
%       force       smoothing force, default 0.07
%       opts        optional settings
%        .pbar      progress bar flag (default: from config file)
%
% Output fields:
%
%       srf         smoothed surface
%       densm       density SMP
%
% Note: this method simply passes to SRF::Morph.

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% global variable for config
global bvqxfile_config;
stc = bvqxfile_config.settings;
if ~isfield(stc, 'Morphing')
    stc = struct('Morphing', struct);
end
stc = stc.Morphing;
if isfield(stc, 'ShowProgress')
    spbar = stc.ShowProgress;
else
    spbar = true;
end
if isfield(stc, 'Smooth')
    stc = stc.Smooth;
else
    stc = struct('Force', 0.07, 'NrOfIterations', 150);
end

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'srf')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to ''%s''.', ...
        mfilename ...
    );
end
if nargin < 2 || ...
   ~isa(niter, 'double') || ...
    numel(niter) ~= 1 || ...
    isnan(niter) || ...
    niter < 0 || ...
    niter > 1e5
    niter = stc.NrOfIterations;
else
    niter = floor(niter);
end
if nargin < 3 || ...
   ~isa(force, 'double') || ...
    numel(force) ~= 1 || ...
    isnan(force) || ...
    force < -16 || ...
    force > 16 || ...
    force == 0
    force = stc.Force;
end
if nargin < 4 || ...
   ~isstruct(opts) || ...
    numel(opts) ~= 1
    opts = struct;
end
if isfield(opts, 'pbar') && ...
    islogical(opts.pbar) && ...
    numel(opts.pbar) == 1
    spbar = opts.pbar;
end

% depending on nargout
if nargout > 1
    [hfile, densm] = ...
        srf_Morph(hfile, niter, force, 'smooth', struct('pbar', spbar));
else
    srf_Morph(hfile, niter, force, 'smooth', struct('pbar', spbar));
end
